package com.newenergy.wisehome.gateway.account;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.broadlink.account.BLAccount;
import cn.com.broadlink.base.BLAppSdkErrCode;
import cn.com.broadlink.base.BLBaseResult;
import cn.com.broadlink.base.BLLoginResult;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 重置密码
 */
public class ResetPasswordFragment extends BaseFragment {

    @BindView(R.id.tv_page)
    TextView mTvPage;

    @BindView(R.id.et_code)
    TextInputEditText mEtCode;
    @BindView(R.id.et_name)
    TextInputEditText mEtName;
    @BindView(R.id.btn_code)
    Button mBtnCode;
    @BindView(R.id.btn_next)
    Button mBtnNext;

    private String mUserName;
    private String mPhoneNum;
    private String mVerifyId;
    private String mTipWhenSucc;
    private String mEmail;
    private String mCountryCode = "0086";

    public static final String USER_NAME = "USER_NAME";

    public static SupportFragment newInstance(String userName) {
        ResetPasswordFragment fragment = new ResetPasswordFragment();
        Bundle args = new Bundle();
        args.putString(USER_NAME, userName);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_reset_password;
    }

    @Override
    public void init(View rootView) {
        mTvPage.setText("重置密码");
    }

    @OnClick({R.id.btn_back,R.id.tv_back,R.id.btn_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
                case R.id.btn_back:
                    doBack();
                    break;
                case R.id.tv_back:
                    doBack();
                    break;
            case R.id.btn_code:
//                if (TextUtils.isEmpty(mUserName)) {
//                    toast("用户名不能为空！");
//                    return;
//                }
//                new RequestVerifyCodeTask().execute();
//                break;
            case R.id.btn_next:
                resetPwd();
                break;
        }
    }

    private void resetPwd() {
        String userPassword = mEtName.getText().toString();
        String vcode = mEtCode.getText().toString();
        if (TextUtils.isEmpty(userPassword) || TextUtils.isEmpty(vcode)) {
            toast("密码不能为空，或验证码不能为空！");
            return;
        }
        new RestPasswordTask().execute(userPassword, vcode);
    }


    @Override
    public void onResume() {
        super.onResume();
        Bundle arguments = getArguments();
        if (arguments != null) {
            mUserName = arguments.getString(USER_NAME);
            Log.i(TAG, "mUserName: " + mUserName);
        }
    }

    class RequestVerifyCodeTask extends AsyncTask<String, Void, BLBaseResult> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected BLBaseResult doInBackground(String... params) {
            return BLAccount.sendRetrieveVCode(mUserName);
        }

        @Override
        protected void onPostExecute(BLBaseResult result) {
            super.onPostExecute(result);
            String msg = null;
            if (result != null && result.getError() == BLAppSdkErrCode.SUCCESS) {
                msg = "获取重置密码的验证码成功";
            } else if (result != null) {
                msg = "获取重置密码的验证码失败： error =" + result.getError() + ", msg = " + result.getMsg();
            } else {
                msg = "获取重置密码的验证码失败：网络错误";
            }
            Log.i(TAG, msg);
            toast(msg);
        }
    }

    //重置密码
    private class RestPasswordTask extends AsyncTask<String, Void, BLLoginResult> {
        private String userPassword;
        private String vcode;
        private BLLoginResult mLoginResult;

        @Override
        protected BLLoginResult doInBackground(String... params) {
            userPassword = params[0];
            vcode = params[1];
            mLoginResult = BLAccount.retrievePassword(mUserName, vcode, userPassword);
            return mLoginResult;
        }

        @Override
        protected void onPostExecute(BLLoginResult result) {
            super.onPostExecute(result);
            if (mLoginResult != null && mLoginResult.getError() == BLAppSdkErrCode.SUCCESS) {
                toast("修改密码成功");
            } else if (mLoginResult != null) {
                String message = "修改密码失败：error = " + mLoginResult.getError() + " ,msg = " + mLoginResult.getMsg();
                toast(message);
            } else {
                toast("修改密码失败：");
            }
        }
    }
}